//
//  XMOriginalStatusView.m
//  XMWeibo
//
//  Created by chi on 14-8-26.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMOriginalStatusView.h"

#import "XMRetweetStatusView.h"
#import "XMStatusFrame.h"
#import "XMStatus.h"
#import "UIImageView+WebCache.h"
#import "XMPhotosView.h"



@interface XMOriginalStatusView ()


#pragma mark - retweetView
/** 用来放置被转发微博内容视图 */
@property (nonatomic, weak) XMRetweetStatusView *retweetView;

#pragma mark - 微博控件
/** 头像 */
@property (nonatomic, weak) UIImageView *iconView;

/** 昵称 */
@property (nonatomic, weak) UILabel *nameLabel;

/** 会员图标 */
@property (nonatomic, weak) UIImageView *vipView;

/** 微博时间 */
@property (nonatomic, weak) UILabel *timeLabel;

/** 微博来源 */
@property (nonatomic, weak) UILabel *sourceLabel;

/** 微博内容 */
@property (nonatomic, weak) UILabel *contentLabel;

/** 微博配图 */
@property (nonatomic, weak) XMPhotosView *photosView;

@end

@implementation XMOriginalStatusView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        //设置背景
        self.highlightedImage = [UIImage stretchableImageWithName:@"timeline_card_top_background_highlighted"];
        self.image = [UIImage stretchableImageWithName:@"timeline_card_top_background"];
        
        // 创建原创微博子控件
        [self setupOriginalSubviews];
        
        //创建转发微博控件
        [self setupRetweetSubviews];
    }
    return self;
}


#pragma mark - 设置模型数据
- (void)setStatusF:(XMStatusFrame *)statusF
{
    //微博头像
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:statusF.status.user.profile_image_url] placeholderImage:[UIImage adaptIOS7ImageWithName:@"avatar_default"]];
    self.iconView.frame = statusF.iconViewF;
    
    //微博昵称
    self.nameLabel.text = statusF.status.user.name;
    self.nameLabel.frame = statusF.nameLabelF;
    if (statusF.status.user.mbtype >= 2) {
        
        self.nameLabel.textColor = [UIColor orangeColor];
        //会员图标
        self.vipView.hidden = NO;
        NSString *img = [NSString stringWithFormat:@"common_icon_membership_level%d", statusF.status.user.mbrank];
        self.vipView.frame = statusF.vipViewF;
        self.vipView.image = [UIImage adaptIOS7ImageWithName:img];
        
    }
    else {
        self.vipView.hidden = YES;
        self.nameLabel.textColor = [UIColor blackColor];
    }
    
    //微博时间
    self.timeLabel.text = statusF.status.statusTime;
    
    
    CGFloat timeLabelX = CGRectGetMaxX(statusF.iconViewF) + kStatusSubViewsMargin;
    CGFloat timeLabelY = CGRectGetMaxY(statusF.nameLabelF) + kStatusSubViewsMargin;
    statusF.timeLabelF = (CGRect){CGPointMake(timeLabelX, timeLabelY), [statusF.status.statusTime sizeWithFont:kStatusTimeLabelFont]};
    self.timeLabel.frame = statusF.timeLabelF;
    
    //微博来源
    CGFloat sourceLabelX = CGRectGetMaxX(statusF.timeLabelF) + kStatusSubViewsMargin;
    CGFloat sourceLabelY = CGRectGetMaxY(statusF.nameLabelF) + kStatusSubViewsMargin;
    statusF.sourceLabelF = (CGRect){CGPointMake(sourceLabelX, sourceLabelY), [statusF.status.source sizeWithFont:kStatusSourceLabelFont]};
    self.sourceLabel.text = statusF.status.source;
    self.sourceLabel.frame = statusF.sourceLabelF;
    
    //微博内容
    self.contentLabel.text = statusF.status.text;
    self.contentLabel.frame = statusF.contentLabelF;

    
    
    //转发微博
    
    if (statusF.status.retweeted_status) {
        self.retweetView.hidden = NO;
        self.photosView.hidden = YES;;
        self.retweetView.frame = statusF.retweetViewF;
        self.retweetView.statusF = statusF;
    }
    else {
        self.retweetView.hidden = YES;
        if (statusF.status.pic_urls.count > 0) {
            self.photosView.hidden = NO;
            self.photosView.photos = statusF.status.pic_urls;
//            [self.photoView sd_setImageWithURL:[NSURL URLWithString:statusF.status.thumbnail_pic]];
            self.photosView.frame = statusF.photoViewF;
        }
        else {
            self.photosView.hidden = YES;;
        }
    }
    
    
}

#pragma mark - 创建原创微博子控件
- (void)setupOriginalSubviews
{
    //头像
    UIImageView *iconView = [[UIImageView alloc]init];
    self.iconView = iconView;
    [self addSubview:iconView];
    
    //昵称
    UILabel *nameLabel = [[UILabel alloc]init];
    self.nameLabel = nameLabel;
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.font = kStatusNameLabelFont;
    [self addSubview:nameLabel];
    
    //vip图标
    UIImageView *vipView = [[UIImageView alloc]init];
    self.vipView = vipView;
    [self addSubview:vipView];
    
    //微博时间
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.textColor = [UIColor orangeColor];
    timeLabel.font = kStatusTimeLabelFont;
    self.timeLabel = timeLabel;
    [self addSubview:timeLabel];
    
    //微博来源
    UILabel *sourceLabel = [[UILabel alloc]init];
    sourceLabel.backgroundColor = [UIColor clearColor];
    sourceLabel.font = kStatusSourceLabelFont;
    sourceLabel.textColor = [UIColor grayColor];
    self.sourceLabel = sourceLabel;
    [self addSubview:sourceLabel];
    
    //微博内容
    UILabel *contentLabel = [[UILabel alloc]init];
    contentLabel.numberOfLines = 0;
    contentLabel.backgroundColor = [UIColor clearColor];
    contentLabel.font = kStatusContentLabelFont;
    self.contentLabel = contentLabel;
    [self addSubview:contentLabel];
    
    //微博配图
    XMPhotosView *photosView = [[XMPhotosView alloc]init];
    self.photosView = photosView;
    [self addSubview:photosView];

}

#pragma mark - 创建转发微博控件
- (void)setupRetweetSubviews
{
    XMRetweetStatusView *retweetView = [[XMRetweetStatusView alloc]init];
    
    self.retweetView = retweetView;
    
    [self addSubview:retweetView];
    
}

@end
